import os
from qiniu import Auth, put_file, etag, urlsafe_base64_encode

access_key = 'wkJupck8Ylduede0ny9KSsIG4vqwDkelgBfejKSv'
secret_key = 'i4UrEdihv93LUun8MMG_OCAcGQHwPU6ZiHq5ijMg'
q = Auth(access_key, secret_key)
bucket_name = 'lixianhe-test'
# 转码是使用的队列名称
pipeline = 'default.sys'
# 遍历文件夹
folder_path = '/Users/lixianhe/Downloads/http'
for root, dirs, files in os.walk(folder_path):
    for file in files:
        localfile = os.path.join(root, file)
        key = 'l/tupian/' + file
        # 可以对转码后的文件进行使用saveas参数自定义命名，当然也可以不指定文件会默认命名并保存在当前空间
        fops = 'imageslim|saveas/' + urlsafe_base64_encode(bucket_name + ':' + key)
        # 在上传策略中指定
        policy = {
            'persistentOps': fops,
            'persistentPipeline': pipeline
        }
        token = q.upload_token(bucket_name, key, 3600, policy)
        ret, info = put_file(token, key, localfile, version='v2')